<?php
namespace  Teachers\Controller;
use Admin\Controller\IndexController;

use Think\Controller;

class PhotosController extends CommonController
{
	public function index(){
		$school_id=$this->showschoolID();
                $type=1;
		$img=M('SchoolImg');
		$data=$this->getimg($school_id);
		$this->assign('imgdata',$data);
		$this->assign('type',$type);
		$this->assign('school_id',$school_id);
		$this->display();
	}
	
	public function addimg(){
		$url_data=$this->upload();
		if(!empty($url_data)){
			$post_data=I('post.');
			$img=M('SchoolImg');
                        $school_id=$this->showschoolID();
			$data=array(
				'school_id'=>$school_id,
				'img_url'=>$url_data['path'],
				'img_type'=>$post_data['img_type'],
				'img_small_url'=>$url_data['thumb_path'],
				'img_description'=>$post_data['img_description'],
				'upload_time'=>time(),
				'update_user'=>$_SESSION['onethink_home']['user_auth']['uid'],
				'upload_type'=>'0',			//0为高校教师上传 1为后台编辑上传
			);
			$img_id=$img->add($data);
			$params['type']=$post_data['img_type'];
			$this->redirect('Teachers/Photos/index',$params, 0, '页面跳转中...');
		}else{
			$this->error('新增失败！');
		}	
		
	}
	
	public function updateimg(){
		$post_data=I('post.');
		if($post_data['img_id']){
			$data=array(
				'img_description'=>$post_data['img_description']
			);
			$map['img_id']=$post_data['img_id'];
			$img=M('SchoolImg');
			$flag=$img->where($map)->save($data);
			$params['type']=$post_data['img_type'];
		} 
		$this->redirect('Teachers/Photos/index',$params, 0, '页面跳转中...');
	}
	
	public function deleteimg(){
		$post_data=I('post.');
		if($post_data['img_id']){
			$map['img_id']=$post_data['img_id'];
			$img=M('SchoolImg');
			$data=$img->where($map)->find();
			unlink('./Public/'.$data['img_small_url']);	//删除图片
			unlink('./Public/'.$data['img_url']);
			$flag=$img->where($map)->delete();
			$params['type']=$post_data['img_type'];
		} 
		$this->redirect('Teachers/Photos/index',$params, 0, '页面跳转中...');
	}

	/**
	 * 以typeid获取类别图片
	 * type 1校园景色	2生活配套	3教学设施	4 360全景
	 * */
	public function getimg($school_id){
		if($school_id){
			$map=array(
				'school_id'		=>	$school_id,
				'upload_type'	=>	'0'
			);
			$img = M('SchoolImg');
			$data=$img->where($map)->select();
		}
		return $data;
	}
	
	
public function upload(){
	
		$upload = new \Think\Upload();// 实例化上传类
	    $upload->maxSize   =     3145728 ;// 设置附件上传大小
	    $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
	    $upload->rootPath  =     './Public/'; // 设置附件上传根目录
	    $upload->savePath  =     'Teachers/images/photos/'; // 设置附件上传（子）目录
	    $upload->saveName  = 	 time().'_'.mt_rand();
	    // 上传文件 
	    if(isset($_FILES['img_url'])){
		    $info = $upload->uploadOne($_FILES['img_url']);
		    if(!$info) {// 上传错误提示错误信息
//		        $this->error($upload->getError());
				return '';
		    }else{// 上传成功
		    	$image = new \Think\Image(); 
				$image->open('./Public/'.$info['savepath'].$info['savename']);
				// 按照原图的比例生成一个最大为60*60的缩略图并保存为thumb.jpg
				$image->thumb(242, 155)->save('./Public/'.$info['savepath'].'thumb_'.$info['savename']);
				$data=array(
					'path'			=>	$info['savepath'].$info['savename'],
					'thumb_path'	=>	$info['savepath'].'thumb_'.$info['savename']
				);
				return $data;
		    }
	    }else{
	    	return '';
	    }    	
	    
	}
}